/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import entity.Fabricante;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
 *
 * @author User
 */
public class FabricanteDAO {
    private Connection connection;
    
    public FabricanteDAO () {
        this.connection = new ConnectionFactory().getConnection();
    }
    
    public void insere(Fabricante fabricante) {
        String sql = "INSERT INTO `fabricante`(`nome`) VALUES (?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, fabricante.getNome());
            stmt.executeUpdate();
            stmt.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
        public void altera (Fabricante fabricante) {       
        String sql = "UPDATE fabricante SET nome=? WHERE idFabricante = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, fabricante.getNome());
            stmt.setInt(2, fabricante.getIdFabricante());
            stmt.executeUpdate();
            stmt.close();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
        
        public boolean nomeExistente(String nome) throws SQLException{
        boolean resultado = false;
        String sql = "SELECT * FROM fabricante WHERE nome=?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, nome);
        ResultSet rs = stmt.executeQuery();
        Fabricante fabricante = new Fabricante();
        if(rs.next()){  
            resultado = true;  
        } 
        return resultado;
        }         
        
        
    public void excluir (Fabricante fabricante) {
        String sql = "DELETE FROM fabricante WHERE idFabricante = ?";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setInt(1, fabricante.getIdFabricante());
            stmt.executeUpdate();
            stmt.close();
        }catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public List<Fabricante> listar() throws SQLException{
            String sql = "SELECT * FROM fabricante";
            List<Fabricante> fabricante = new ArrayList();       
            PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();            
            while(rs.next()){
                Fabricante fab = new Fabricante();
                fab.setNome(rs.getString("nome"));
                fab.setIdFabricante(rs.getInt("idFabricante"));
                fabricante.add(fab);
            }
            return fabricante; 
    }
  
    public Fabricante buscaFab(int idFab) throws SQLException{
        String sql = "SELECT * FROM fabricante WHERE idFabricante=?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setInt(1, idFab);
        ResultSet rs = stmt.executeQuery();
        Fabricante fabricante = new Fabricante();
        while(rs.next()){            
            fabricante.setNome(rs.getString("nome"));
            fabricante.setIdFabricante(rs.getInt("idFabricante"));         
        }
        return fabricante;
        }
    
        public List<Fabricante> procurar(String str) throws SQLException{
        String sql = "SELECT nome, idFabricante FROM fabricante WHERE nome LIKE ?";
        PreparedStatement stmt = connection.prepareStatement(sql);
        stmt.setString(1, "%"+str+"%");        
        List<Fabricante> fabricante = new ArrayList();
        ResultSet rs = stmt.executeQuery();        
        while (rs.next()) {
            Fabricante cat = new Fabricante();
            cat.setNome(rs.getString("nome"));
            cat.setIdFabricante(rs.getInt("idFabricante"));
            fabricante.add(cat);
        }
        stmt.close();
        return fabricante;
    }
    
        
}
